package cn.edu.zstu.overlappingdisappear.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.List;

import cn.edu.zstu.overlappingdisappear.R;
import cn.edu.zstu.overlappingdisappear.base.BaseActivity;
import cn.edu.zstu.overlappingdisappear.bean.LeverBean;
import cn.edu.zstu.overlappingdisappear.fragment.LeverFragment;
import cn.edu.zstu.overlappingdisappear.sqlite.ChainDao;

/**
 * @Author ZhuMingWei(986990501@qq.com)
 * @Date on 2019/11/6
 * @Version v1.0.0
 * @Company TODO
 * @Describle TODO
 */

public class LevelActivity extends BaseActivity implements View.OnClickListener {

    private ChainDao mChainDao;

    private ImageView btnBack;
    private Button[] btnDifficults;
    private LeverFragment leverFragment;
    private int curDifficult = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level);
        initView();
    }

    private void initView() {
        FragmentManager fm = getSupportFragmentManager();
        leverFragment = (LeverFragment) fm.findFragmentById(R.id.fragment_lever);
        btnBack = findViewById(R.id.btn_back);
        btnDifficults = new Button[4];
        btnDifficults[0] = findViewById(R.id.btn_easy);
        btnDifficults[1] = findViewById(R.id.btn_normal);
        btnDifficults[2] = findViewById(R.id.btn_difficult);
        btnDifficults[3] = findViewById(R.id.btn_limit);
        btnBack.setOnClickListener(this);
        btnDifficults[0].setOnClickListener(this);
        btnDifficults[1].setOnClickListener(this);
        btnDifficults[2].setOnClickListener(this);
        btnDifficults[3].setOnClickListener(this);
        mChainDao = new ChainDao(this);
        btnDifficults[0].performClick();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mChainDao != null)
            mChainDao.close();
    }

    @Override
    public void onClick(View view) {
        List<LeverBean> levers;
        switch (view.getId()){
            case  R.id.btn_back:
                finish();
                break;
            case R.id.btn_easy:
                if(0 == curDifficult)   break;
                if(-1 != curDifficult){
                    btnDifficults[curDifficult].setBackgroundResource(R.mipmap.bg_btn_difficult_unclick);
                    btnDifficults[curDifficult].setTextColor(Color.WHITE);
                }
                levers = mChainDao.queryLevers(1);
                leverFragment.initDatas(levers,1);
                curDifficult = 0;
                btnDifficults[curDifficult].setBackgroundResource(R.mipmap.bg_btn_difficult_click);
                btnDifficults[curDifficult].setTextColor(Color.BLACK);
                break;
            case R.id.btn_normal:
                if(1 == curDifficult)   break;
                if(-1 != curDifficult){
                    btnDifficults[curDifficult].setBackgroundResource(R.mipmap.bg_btn_difficult_unclick);
                    btnDifficults[curDifficult].setTextColor(Color.WHITE);
                }
                levers = mChainDao.queryLevers(2);
                leverFragment.initDatas(levers,2);
                curDifficult = 1;
                btnDifficults[curDifficult].setBackgroundResource(R.mipmap.bg_btn_difficult_click);
                btnDifficults[curDifficult].setTextColor(Color.BLACK);
                break;
            case R.id.btn_difficult:
                if(2 == curDifficult)   break;
                if(-1 != curDifficult){
                    btnDifficults[curDifficult].setBackgroundResource(R.mipmap.bg_btn_difficult_unclick);
                    btnDifficults[curDifficult].setTextColor(Color.WHITE);
                }
                levers = mChainDao.queryLevers(3);
                leverFragment.initDatas(levers,3);
                curDifficult = 2;
                btnDifficults[curDifficult].setBackgroundResource(R.mipmap.bg_btn_difficult_click);
                btnDifficults[curDifficult].setTextColor(Color.BLACK);
                break;
            case R.id.btn_limit:
                break;
        }
    }
}
